package com.open.jacekkowalczyk;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.apache.log4j.Logger;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

/**
 * Xml2Map!
 */
public class Xml2Map {

    private static Logger LOGGER = Logger.getLogger(Xml2Map.class);

    public List<Map<String, String>> getListOfLevel1MapElelements(final File file, final String rootElemName,
        final String firstLevelElemName) {

        final List<Map<String, String>> elements = new ArrayList<Map<String, String>>();

        try {
            final DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
            DocumentBuilder db;

            db = dbf.newDocumentBuilder();

            final Document doc = db.parse(file);
            doc.getDocumentElement().normalize();
            // System.out.println("Root element " + doc.getDocumentElement().getNodeName());
            final NodeList rootNodeList = doc.getElementsByTagName(rootElemName);

            LOGGER.debug("all elements");
            for (int i = 0; i < rootNodeList.getLength(); i++) {
                final Node rootXmlNode = rootNodeList.item(i);
                LOGGER.debug("root: " + rootXmlNode.getNodeName());

                if (rootXmlNode.getNodeType() == Node.ELEMENT_NODE) {
                    final Element rootXmlElem = (Element)rootXmlNode;
                    final NodeList level1Nodes = rootXmlElem.getElementsByTagName(firstLevelElemName);
                    for (int j = 0; j < level1Nodes.getLength(); j++) {
                        final Node level1Node = level1Nodes.item(j);
                        LOGGER.debug("1stlevel:    " + level1Node.getNodeName());

                        if (level1Node.getNodeType() == Node.ELEMENT_NODE) {
                            final Map<String, String> level1XmlElementsMap = new HashMap<String, String>();
                            final Element level1NodeElem = (Element)level1Node;
                            final NodeList level2Nodes = level1NodeElem.getChildNodes();

                            for (int k = 0; k < level2Nodes.getLength(); k++) {
                                final Node level2Node = level2Nodes.item(k);
                                // System.out.println("2ndlevelNode:    " + level2Node.getNodeName());

                                if (level2Node.getNodeType() == Node.ELEMENT_NODE) {
                                    final Element level2NodeElem = (Element)level2Node;
                                    LOGGER.debug("2ndlevelElem:    " + level2NodeElem.getNodeName() + ": "
                                        + level2NodeElem.getTextContent());
                                    level1XmlElementsMap.put(level2NodeElem.getNodeName(), level2NodeElem.getTextContent());
                                }
                            }
                            elements.add(level1XmlElementsMap);
                        }

                    }
                }

            }
            return elements;
        } catch (final ParserConfigurationException e) {
            e.printStackTrace();
            return elements;
        } catch (final SAXException e) {
            e.printStackTrace();
            return elements;
        } catch (final IOException e) {
            e.printStackTrace();
            return elements;
        }
    }
}
